package controller;


import entity.Article;
import entity.PageEntity;
import entity.User;
import service.ArticleService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

@WebServlet("/article/*")
public class ArticleServlet extends BaseServlet{

    private ArticleService articleService = new ArticleService();

    /**
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void findPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        Map<String,String> param = getParam(req);

        //当前页的数据
        Integer currPage = Integer.parseInt(param.get("currPage"));

        //搜索条件
        String title = param.get("title");

        PageEntity<Article> pageData = articleService.findPage(title,currPage);
        req.setAttribute("pageData", pageData);
        req.setAttribute("title", title);
        req.getRequestDispatcher("/html/article.jsp").forward(req, resp);

    }

    /**
     * 发布文章
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //获取前端所有的代码
        req.setCharacterEncoding("utf-8");
        Map<String,String> param = getParam(req);

        String title = param.get("title");
        String content = param.get("content");

        //将信息封装为一个文章对象
        Article article = new Article(null,title,content,0L,new Date(),loginUser.getRealName(),loginUser.getId());

        articleService.save(article);

        resp.sendRedirect("/article/findPage?currPage=1");


    }

    /**
     *查询文章详情
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void articleDetail(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //文章id
        Long id = Long.parseLong(req.getParameter("id"));

        //浏览量+1
        articleService.updateBrowseCount(id);

        //文章对象
        Article article = articleService.findById(id);

        //收藏数量
        Integer favoriteCount = articleService.findFavoriteCount(id);

        //我是否收藏过这篇文章
        Boolean isFavorite = articleService.isFavorite(loginUser.getId(),id);

        //我关注的人里面也收藏这篇文章的人
        List<User> favoriteList = articleService.findFavoriteList(loginUser.getId(),id);


        req.setAttribute("article", article);
        req.setAttribute("favoriteCount", favoriteCount);
        req.setAttribute("isFavorite", isFavorite);
        req.setAttribute("favoriteList", favoriteList);

        req.getRequestDispatcher("/html/article_detail.jsp").forward(req, resp);


    }

    /**
     *收藏/取消收藏
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void addFavorite(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        Long id = Long.parseLong(req.getParameter("id"));

        Boolean favorite = articleService.isFavorite(loginUser.getId(),id);

        if(favorite){
            //收藏了文章，取消收藏
            articleService.delFavorite(loginUser.getId(),id);
        }else {
            //我之前没有收藏过这篇文章，我是来收藏文章的
            articleService.addFavorite(loginUser.getId(),id);
        }
    }

    /**
     * 我的收藏+搜索+分页
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void findFavoritePage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //获取前端所有的数据
        Map<String,String> param = getParam(req);

        Integer currPage = Integer.parseInt(param.get("currPage"));
        String title = param.get("title");

        //查询分页数据
        PageEntity<Article>pageData = articleService.findFavoritePage(title,loginUser.getId(),currPage);

        req.setAttribute("pageData", pageData);
        req.setAttribute("title", title);

        req.getRequestDispatcher("/html/article_collect.jsp").forward(req, resp);

    }

}
